package biz.alla.market.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import biz.alla.market.dao.ApplicationDao;
import biz.alla.market.domain.Application;

@SuppressWarnings("unchecked")
public class ApplicationDaoImpl extends HibernateDaoSupport implements
		ApplicationDao {

	@Override
	public List<Application> getApplications(int categoryId) {
		List<Application> applicationList = new ArrayList<Application>();
		Criteria criteria = createCriteria();
		criteria.add(Restrictions.eq("categoryId", categoryId));
		applicationList.addAll(criteria.list());
		return applicationList;
	}

	@Override
	public List<Application> getApplications(int categoryId, int subCategoryId) {
		List<Application> applicationList = new ArrayList<Application>();
		Criteria criteria = createCriteria();
		criteria.add(Restrictions.eq("categoryId", categoryId));
		criteria.add(Restrictions.eq("subCategoryId", subCategoryId));
		applicationList.addAll(criteria.list());
		return applicationList;
	}

	@Override
	public Application getApplication(int applicationId) {
		Criteria criteria = createCriteria();
		criteria.add(Restrictions.eq("id", applicationId));
		return (Application) criteria.uniqueResult();
	}

	private Criteria createCriteria() {
		return getSession().createCriteria(Application.class);
	}
}
